package com.whfc.ms.api.emp.controller;

import com.whfc.common.mqtt.MqttMessageParam;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.entity.AppDeviceCardLog;
import com.whfc.emp.param.AddEmpDataParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppEmpInnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-06-28
 */
@RestController
@RequestMapping("/ms/api/inner")
public class AppEmpInnerController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppEmpInnerService msAppEmpInnerService;

    @PostMapping("/buildEmpData")
    public Result buildEmpData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("empId") Integer empId,
                               @RequestParam("deviceId") Integer deviceId,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("time") Date time,
                               @RequestParam("lat") Double lat,
                               @RequestParam("lng") Double lng) {
        logger.info("编造人员安全帽数据，loginer:{},empId:{},deviceId:{},lat:{},lng:{}",
                user.getUsername(), empId, deviceId, lat, lng);
        msAppEmpInnerService.buildEmpData(empId, deviceId, time, lat, lng);
        return ResultUtil.success();
    }


    @PostMapping("/updateEmpInfo")
    public Result updateEmpInfo(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("file") MultipartFile file) throws Exception {
        logger.info("导入excel根据身份证修改人员信息,loginer:{}", user.getUsername());
        msAppEmpInnerService.updateEmpInfo(file);
        return ResultUtil.success();
    }

    @PostMapping("/syncWorkTypeId")
    public Result syncWorkTypeId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("deptId") Integer deptId) {
        logger.info("处理工种id内部接口,loginer:{},deptId:{}", user.getUsername(), deptId);
        msAppEmpInnerService.syncWorkTypeId(deptId);
        return ResultUtil.success();
    }

    @PostMapping("/refreshPolygonWKT")
    public Result refreshPolygonWKT(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("deptId") Integer deptId) {
        logger.info("刷新人员polygonWKT内部接口,loginer:{},deptId:{}", user.getUsername(), deptId);
        msAppEmpInnerService.refreshPolygonWKT(deptId);
        return ResultUtil.success();
    }

    @PostMapping("/amendHelmetData")
    public Result amendHelmetData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestBody List<AppDeviceCardLog> param) {
        logger.info("修正安全帽数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppEmpInnerService.amendHelmetData(param);
        return ResultUtil.success();
    }

    @PostMapping(value = "/addEmpData", params = {"empId"})
    public Result addEmpData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("time") Date time,
                             @RequestParam("empId") Integer empId,
                             @RequestParam("deviceId") Integer deviceId,
                             @RequestParam("lat") Double lat,
                             @RequestParam("lng") Double lng) {
        logger.info("添加安全帽数据,loginer:{},time:{},empId:{},deviceId:{},lat:{},lng:{}",
                user.getUsername(), time, empId, deviceId, lat, lng);
        msAppEmpInnerService.addEmpData(time, empId, deviceId, lat, lng);
        return ResultUtil.success();
    }

    @PostMapping("/addEmpData")
    public Result addEmpData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @Validated @RequestBody AddEmpDataParam param) {
        logger.info("批量添加安全帽数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppEmpInnerService.batchAddEmpData(param);
        return ResultUtil.success();
    }

    @PostMapping("/facegate/record/clean")
    public Result facegateRecordClean(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {
        logger.info("清理闸机识别记录脏数据,loginUser:{},startTime:{},endTime:{}", user.getUsername(), startTime, endTime);
        msAppEmpInnerService.cleanFacefateRecord(startTime, endTime);
        return ResultUtil.success();
    }

    @PostMapping("/attend/record/clean")
    public Result attendRecordClean(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {
        logger.info("清理人员考勤记录脏数据,loginUser:{},startTime:{},endTime:{}", user.getUsername(), startTime, endTime);
        msAppEmpInnerService.cleanEmpAttendRecord(startTime, endTime);
        return ResultUtil.success();
    }

    @PostMapping("/send/mqtt/message")
    public Result setMqttMessage(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestBody MqttMessageParam param) {
        logger.info("发送MQTT消息, loginUser:{}, param:{}", user.getUsername(), param);
        msAppEmpInnerService.sendMqttMessage(param);
        return ResultUtil.success();
    }

    @PostMapping("/update/workType")
    public Result updateEmpWorkType(@SessionAttribute(SessionAttr.MS_USER) SysUser user){
        logger.info("更新人员工种问题, loginUser:{}", user.getUsername());
        msAppEmpInnerService.updateEmpWorkType();
        return ResultUtil.success();
    }
}
